// promise  | 生成器  | async/await

// promise
function bar() {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('bar1');
      resolve();
    }, 100);
  });
}

function bar2() {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('bar2');
      resolve();
    }, 100);
  });
}

bar().then(() => bar2());

// 生成器
let i = 0;
const consoleNumber = () => {
  setTimeout(() => {
    console.log(`生成器${i++}`);
  }, 100);
};
function* foo() {
  const result1 = yield consoleNumber();
  const result2 = yield consoleNumber();
  console.log('生成器foo函数执行完毕');
}
const task = foo();
task.next();
task.next();
task.next();

// async/await
async function foo2() {
  await new Promise((resolve) => {
    setTimeout(() => {
      console.log('1');
      resolve();
    }, 100);
  });
  await new Promise((resolve) => {
    setTimeout(() => {
      console.log('2');
      resolve();
    }, 100);
  });
  await new Promise((resolve) => {
    setTimeout(() => {
      console.log('3');
      resolve();
    }, 100);
  });
}

foo2();
